package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Autowired
    DishMapper dishMapper;

    @Transactional
    @Override
    public void saveWithSetMealDish(SetmealDTO setmealDTO) {
        //添加一条套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        //添加n条套餐与菜品关系
         Long setmealId = setmeal.getId();
         List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
         if (setmealDishes!=null&&setmealDishes.size()!=0)
         {
             setmealDishes.forEach(s->{
                 s.setSetmealId(setmealId);
             });
             setmealDishMapper.insertBatch(setmealDishes);
         }


    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setmealMapper.page(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
        //启售的套餐不能删除
        for (Long id:ids)
        {
           Setmeal setmeal= setmealMapper.getById(id);
           if (setmeal.getStatus()==1)
           {
               throw new DeletionNotAllowedException("启售的套餐不可以删除");
           }
        }
        //删除套餐记录
         setmealMapper.deleteBatch(ids);
        //删除套餐_菜品关系记录
         setmealDishMapper.deleteBatch(ids);


    }

    @Override
    public SetmealVO getByIdWithSetmeal(Long id) {
        //查询setmeal表
        Setmeal setmeal= setmealMapper.getById(id);
        //查询setmeal_dish表
        List<SetmealDish> setmealDishs=setmealDishMapper.getBySetmealId(id);
        //封装setmealVo
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishs);
        return setmealVO;
    }

    @Override
    public void updateWithSetmeal(SetmealDTO setmealDTO) {
        //修改Setmeal表
         Setmeal setmeal = new Setmeal();
         BeanUtils.copyProperties(setmealDTO,setmeal);
         setmealMapper.update(setmeal);
        //删除原来的Setmeal_dish关系记录
        setmealDishMapper.deleteBySetmealId(setmeal.getId());
        //添加新的Setmeal_dish关系记录
         List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
         if (setmealDishes!=null&&setmealDishes.size()!=0)
         {
             setmealDishes.forEach(s->{
                    s.setSetmealId(setmeal.getId());
             });
             setmealDishMapper.insertBatch(setmealDishes);
         }


    }

    @Override
    public void status(Integer status,Long id) {
        Setmeal setmeal = Setmeal.builder().status(status).id(id).build();
        //判断该套餐是否包含停售菜品
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        List<Long> dishId=new ArrayList<>();
        setmealDishes.forEach(s->{
            dishId.add(s.getDishId());
        });
        List<Dish> dishes = dishMapper.getByIds(dishId);
        if (dishes!=null&&dishes.size()!=0)
        {
            for (Dish dish:dishes)
            {
                if (dish.getStatus()==0)
                {
                    throw new SetmealEnableFailedException("套餐包含停售菜品");
                }
            }
        }

        //修改状态
        setmealMapper.update(setmeal);
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }


}
